package com.zh.work.controller.platform.virtual;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zh.common.entity.JSONResult;
import com.zh.work.dao.platform.virtual.VirtualCardMapper;
import com.zh.work.dao.platform.virtual.VirtualRechargeTransMapper;
import com.zh.work.entity.virtual.VirtualCardEntity;
import com.zh.work.entity.virtual.VirtualRechargeTrans;
import com.zh.work.entity.virtual.VirtualRechargeTransH;
import com.zh.work.service.platform.virtual.VirtualRechargeTransService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * code is far away from bug with the animal protecting
 * ┏┓　　　┏┓
 * ┏┛┻━━━┛┻┓
 * ┃　　　　　　　┃
 * ┃　　　━　　　┃
 * ┃　┳┛　┗┳　┃
 * ┃　　　　　　　┃
 * ┃　　　┻　　　┃
 * ┃　　　　　　　┃
 * ┗━┓　　　┏━┛
 * 　　┃　　　┃神兽保佑
 * 　　┃　　　┃代码无BUG！
 * 　　┃　　　┗━━━┓
 * 　　┃　　　　　　　┣┓
 * 　　┃　　　　　　　┏┛
 * 　　┗┓┓┏━┳┓┏┛
 * 　　　┃┫┫　┃┫┫
 * 　　　┗┻┛　┗┻┛
 *
 * @Description: 账户充值信息管理
 * @Author: Li.HuiNeng
 * @Create in: 2019/09/11
 * @Version: 1.0.1
 */
@Api(value = "账户充值信息管理控制器", produces = "application/json", tags = {"账户充值信息管理控制器"})
@RestController
@RequestMapping("/virtualRechargeTrans")
public class VirtualRechargeTransController {

    /**
     * @Description: 简易crud时使用
     * ------------------------
     * @Author: Li.HuiNeng
     * @Date: 2019/09/11
     */
    @Autowired
    private VirtualRechargeTransMapper mapper;

    /**
     * @Description: 有业务逻辑需要处理时使用
     * ------------------------
     * @Author: Li.HuiNeng
     * @Date: 2019/09/11
     */
    @Autowired
    private VirtualRechargeTransService service;


    @Autowired
    private VirtualCardMapper cardMapper;

    /**
     * @Description: redis实例
     * ------------------------
     * @Author: Li.HuiNeng
     * @Date: 2019/09/11
     */
    @Autowired
    private RedisTemplate redisTemplate;

    @ApiOperation(value = "单笔充值", tags = {"putAddVirtualRechargeTrans"}, notes = "单笔充值")
    @PutMapping("/putAddVirtualRechargeTrans")
    @ResponseBody
    public JSONResult<Object> putAddVirtualRechargeTrans(@ApiParam(value = "账户信息实体类", name = "virtualCardEntity")
                                                   @RequestBody VirtualCardEntity virtualCardEntity) {
        JSONResult<Object> jsonResult = new JSONResult<>();
        try {
            if (service.putModVirtualCardTrans(virtualCardEntity) > 0) {
                jsonResult.setCode(200);
                jsonResult.setMessage("添加完成");
                jsonResult.setSuccess(true);
            } else {
                jsonResult.setMessage("添加失败,请检查参数是否填写完整!");
                jsonResult.setSuccess(false);
            }
        } catch (Exception e) {
            jsonResult.setCode(e.hashCode());
            jsonResult.setSuccess(false);
            jsonResult.setMessage(e.getLocalizedMessage());
            e.printStackTrace();
        }
        return jsonResult;
    }


    @ApiOperation(value = "根据id查询详细信息", tags = {"getVirtualRechargeTransById"}, notes = "根据id查询详细信息")
    @GetMapping("/getVirtualRechargeTransById")
    public JSONResult<VirtualRechargeTrans> getVirtualRechargeTransById(@ApiParam(value = "账户充值信息实体类", name = "VirtualRechargeTrans") VirtualRechargeTrans virtualRechargeTrans) {
        JSONResult<VirtualRechargeTrans> jsonResult = new JSONResult<>();
        try {
            VirtualRechargeTrans result = (VirtualRechargeTrans)virtualRechargeTrans.selectById(virtualRechargeTrans.getId());
            if (result != null) {
                jsonResult.setCode(200);
                jsonResult.setMessage("查询完成");
                jsonResult.setSuccess(true);
                jsonResult.setData(result);
            } else {
                jsonResult.setMessage("未查询到相应信息，请检查参数是否正确");
                jsonResult.setSuccess(false);
            }
        } catch (Exception e) {
            jsonResult.setSuccess(false);
            jsonResult.setMessage(e.getLocalizedMessage());
            jsonResult.setCode(e.hashCode());
            e.printStackTrace();
        }
        return jsonResult;
    }

    @ApiOperation(value = "查询交易金额统计", tags = {"getRechargeStatistic"}, notes = "查询交易金额统计")
    @GetMapping("/getRechargeStatistic")
    public JSONResult<Object> getRechargeStatistic(@RequestParam Integer type) {
        JSONResult<Object> jsonResult = new JSONResult<>();
        Map<String,Object> map = new HashMap<>();
        try {
            if (type == 1) map = this.cardMapper.getRechargeStatistic();
            else map = this.cardMapper.getRechargeStatisticH();
            if (map != null) {
                jsonResult.setCode(200);
                jsonResult.setMessage("查询完成");
                jsonResult.setSuccess(true);
                jsonResult.setData(map);
            } else {
                jsonResult.setMessage("未查询到相应信息，请检查参数是否正确");
                jsonResult.setSuccess(false);
            }
        } catch (Exception e) {
            jsonResult.setSuccess(false);
            jsonResult.setMessage(e.getLocalizedMessage());
            jsonResult.setCode(e.hashCode());
            e.printStackTrace();
        }
        return jsonResult;
    }


    @ApiOperation(value = "分页查询参数数据集", tags = {"getPagesVirtualRechargeTrans"}, notes = "分页查询参数数据集")
    @GetMapping("/getPagesVirtualRechargeTrans")
    public JSONResult<List<VirtualRechargeTrans>> getPagesVirtualRechargeTrans(
            @ApiParam(value = "起始页", name = "pageNo")Integer pageNo,
            @ApiParam(value = "当前页显示数量", name = "pageSize")Integer pageSize,
            @ApiParam(value = "账户信息实体类", name = "VirtualRechargeTrans") VirtualRechargeTrans virtualRechargeTrans) {
        JSONResult<List<VirtualRechargeTrans>> jsonResult = new JSONResult<>();
        try {
            IPage<VirtualRechargeTrans> result = this.service.selectVirtualRechargeTransPages(new Page<>(pageNo, pageSize), virtualRechargeTrans);
            jsonResult.setTotal(result.getTotal());
            jsonResult.setData(result.getRecords());
            jsonResult.setMessage("操作成功");
            jsonResult.setSuccess(true);
            jsonResult.setCode(200);
        } catch (Exception e) {
            jsonResult.setMessage(e.getLocalizedMessage());
            jsonResult.setSuccess(false);
            e.printStackTrace();
        }
        return jsonResult;
    }

    @ApiOperation(value = "分页查询历史参数数据集", tags = {"getPagesVirtualRechargeTransH"}, notes = "分页查询历史参数数据集")
    @GetMapping("/getPagesVirtualRechargeTransH")
    public JSONResult<List<VirtualRechargeTransH>> getPagesVirtualRechargeTransH(
            @ApiParam(value = "起始页", name = "pageNo")Integer pageNo,
            @ApiParam(value = "当前页显示数量", name = "pageSize")Integer pageSize,
            @ApiParam(value = "账户信息实体类", name = "VirtualRechargeTrans") VirtualRechargeTransH virtualRechargeTransH) {
        JSONResult<List<VirtualRechargeTransH>> jsonResult = new JSONResult<>();
        try {
            IPage<VirtualRechargeTransH> result = this.service.selectVirtualRechargeTransHPages(new Page<>(pageNo, pageSize), virtualRechargeTransH);
            jsonResult.setTotal(result.getTotal());
            jsonResult.setData(result.getRecords());
            jsonResult.setMessage("操作成功");
            jsonResult.setSuccess(true);
            jsonResult.setCode(200);
        } catch (Exception e) {
            jsonResult.setMessage(e.getLocalizedMessage());
            jsonResult.setSuccess(false);
            e.printStackTrace();
        }
        return jsonResult;
    }

    @ApiOperation(value = "批量充值", tags = {"putAddBatchVirtualRechargeTrans"}, notes = "批量充值")
    @PostMapping("/putAddBatchVirtualRechargeTrans")
    @ResponseBody
    public JSONResult<Object> putAddBatchVirtualRechargeTrans(@RequestBody MultipartFile file) {
        JSONResult<Object> jsonResult = new JSONResult<>();
        try {
            Map<String,Object> map = service.putAddBatchVirtualRechargeTrans(file);
            if ((Boolean) map.get("success")) {
                jsonResult.setMessage(map.get("msg").toString());
                jsonResult.setSuccess(true);
            } else {
                if (map.get("excelErr") != null) {
                    jsonResult.setMessage(map.get("excelErr").toString());
                    jsonResult.setSuccess(false);
                    return jsonResult;
                }
            }
        } catch (Exception e) {
            jsonResult.setCode(e.hashCode());
            jsonResult.setSuccess(false);
            jsonResult.setMessage(e.getLocalizedMessage());
            e.printStackTrace();
        }
        return jsonResult;
    }
}
